/**
 * @param {string} s
 * @return {string}
 */
var longestPalindrome = function(s) {
  if(s.length <= 1) {
    return s;
  }
  let length = 1, tempStr = "", resultStr = s[0];
  for(let i = 1; i < s.length; i++) {
    let j = i - 1, k = i + 1;
    tempStr = s[i];
    while(j >= 0 && s[j] == s[i]) {
      tempStr = s[j] + tempStr;
      j--;
    }
    while(k < s.length && s[k] == s[i]) {
      tempStr = tempStr + s[k];
      k++;
    }
    while(j >= 0 && k < s.length && s[j] == s[k]) {
      tempStr = s[j] + tempStr + s[k];
      j--;
      k++;
    }
    if(tempStr.length > length) {
      length = tempStr.length;
      resultStr = tempStr;
    }
  }
  return resultStr;
};
let s = "babad";
console.log(longestPalindrome(s));
s = "cbbd";
console.log(longestPalindrome(s));
s = "ac";
console.log(longestPalindrome(s));
